﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using visinia.SmartEngine;
using visinia.SmartEngine.Installer;
using visinia.SmartEngine.Site;

namespace visinia.web.admin.pages
{
    public partial class Themes : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string path = Server.MapPath(WebRoots.GetThemesRoot);
                string[] themeDirs = Directory.GetFileSystemEntries(path);
                List<ThemeData> themes = new List<ThemeData>();
                foreach (string dir in themeDirs)
                {
                    string themename = dir.Substring((dir.LastIndexOf('\\') + 1));
                    if (!File.Exists(dir + "\\page.master"))
                        continue;
                    string file = dir + "\\Theme.Data";
                    if (File.Exists(file))
                    {
                        ThemeData data = new ThemeData();
                        data.dirname = themename;
                        XmlDocument doc = new XmlDocument();
                        doc.Load(file);
                        XmlNode pagesroot = doc.DocumentElement;
                        if (pagesroot != null)
                        {
                            foreach (XmlNode child in pagesroot.ChildNodes)
                            {
                                string name = child.Name;
                                if (name.ToLower() == "name")
                                    data.name = child.InnerText;
                                if (name.ToLower() == "author")
                                    data.author = child.InnerText;
                                if (name.ToLower() == "website")
                                    data.website = child.InnerText;
                                if (name.ToLower() == "version")
                                    data.version = child.InnerText;
                                if (name.ToLower() == "description")
                                    data.description = child.InnerText;
                                if (name.ToLower() == "image")
                                    data.image = WebRoots.GetThemesRoot + themename + "\\" + child.InnerText;
                            }
                        }
                        themes.Add(data);
                    }
                }
                //get current theme data and then remove current theme from the list
                ThemeData currentTheme = themes.Find(p => string.Compare(p.dirname, SiteSettings.Theme, true) == 0);
                themes.Remove(currentTheme);
                THEMES.DataSource = themes;
                THEMES.DataBind();
                if (currentTheme != null)
                {
                    CurrentThemeName.Text = currentTheme.name;
                    CurrentThemeVersion.Text = currentTheme.version;
                    CurrentThemeAuthor.Text = currentTheme.author;
                    CurrentThemeAuthor.NavigateUrl = currentTheme.website;
                    CurrentThemeImage.Src = currentTheme.image;
                    CurrentThemeDescription.Text = currentTheme.description;
                }
            }
        }

        private class ThemeData
        {
            private string _dirname;
            public string dirname { get { return _dirname; } set { _dirname = value; } }

            private string _name;
            public string name { get { return _name; } set { _name = value; } }

            private string _author;
            public string author { get { return _author; } set { _author = value; } }

            private string _website;
            public string website { get { return _website; } set { _website = value; } }

            private string _version;
            public string version { get { return _version; } set { _version = value; } }

            private string _description;
            public string description { get { return _description; } set { _description = value; } }

            private string _image;
            public string image { get { return _image; } set { _image = value; } }
        }

        protected void THEMES_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
        {
            if (e.CommandName == "Activate")
            {
                SiteSettings.Theme = (string)e.CommandArgument;
                Response.Redirect(Request.RawUrl);
            }
            if (e.CommandName == "DleteTheme")
            {
                InstallController.UnInstallTheme((string)e.CommandArgument);
                Response.Redirect(Request.RawUrl);
            }
        }
        protected void NEW_THEME_Click(object sender, EventArgs e)
        {
            Response.Redirect(SystemMenu.CallPage("_Install", "Theme"));
        }
    }
}